package med.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import med.config.Config;
import med.extract.Stemmer;

public class Reader {

	public static List<String> getTextFromDirectory(File dir) {

		List<String> text = new LinkedList<String>();

		if (dir.isDirectory()) {
			for (File file : dir.listFiles()) {
				if (file.isFile()) {
					text.addAll(Reader.getTextFromFile(file));
				}
			}
		} else {
			throw new IllegalArgumentException(dir.getName()
					+ " is not a directory");
		}

		return text;
	}

	public static List<String> getTextFromDirectory(String dirName) {
		File dir = new File(dirName);
		return getTextFromDirectory(dir);
	}

	public static List<String> getTextFromFile(File file) {
		BufferedReader rdr;
		StringBuilder sb = new StringBuilder();

		try {
			rdr = new BufferedReader(new FileReader(file));
			String line;
			while ((line = rdr.readLine()) != null) {
				sb.append(line + ' ');
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		List<String> result = new LinkedList<String>();
		
		boolean useStemmer = Config.getInstance().isUseStemmerParam();
		
		for (String token : sb.toString().split("\\s+")) {
			token = process(token, useStemmer);
			if (!isSkippable(token)) {
				result.add(token);
			}
		}
		
		return result;
	}

	public static List<String> getTextFromFile(String fileName) {
		return getTextFromFile(new File(fileName));
	}
	
	public static String process(String token, boolean useStemmer) {
		token = token.replace((char)160, ' '); // replaces NBSP characters which are non-whitespace
		if (useStemmer) {
			token = stem(token);
		}
		return token.trim(); //TODO expand
	}

	public static boolean isSkippable(String token) {
		return token.isEmpty() || token.length() < 2;
	}

	private static String stem(String tokenStr) {
		char[] token = tokenStr.toCharArray();
		final Stemmer stemmer = new Stemmer(token, token.length);
		stemmer.stem();
		return stemmer.toString();
	}
}
